﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Gollie.DataTables;

namespace Gollie.Controls
{
	public partial class OffersPerVendor : BaseUserControl
	{
		bool isCustomer;
		private uint vendorId;
		VendorInfo vendorInfo = null;

		protected void Page_Load(object sender, EventArgs e)
		{
		}

		protected override void PreLoadHandler(object sender, EventArgs e)
		{
			try
			{
				vendorId = Convert.ToUInt32(this.Request["v"]);

				vendorInfo = VendorInfo.GetVendorInfo(vendorId);
			}
			catch
			{
				Response.End();
			}

			isCustomer = (this.IsLoggedIn && this.UserInfo.Role == Role.Customer);
		}

		protected override void OnPostBack(object sender, EventArgs e)
		{
			this.CustomizeView();
		}

		protected override void OnFirstHit(object sender, EventArgs e)
		{
			this.OrderBasketControl.GridView.DataSource = this.OrderBasketData;
			this.OrderBasketControl.GridView.DataBind();

			ActiveOffersPerVendor offers = ActiveOffersPerVendor.Instance(vendorId);

			this.GridView1.DataSource = offers; //.DefaultView;

			this.GridView1.DataBind();

			this.CustomizeView();

			this.SetNumberOrdered();
		}

		private void CustomizeView()
		{
			this.lblVendorName.Text = this.vendorInfo.BedrijfsNaam;

			this.VendorPresenterFull.VendorId = vendorId;

			string headerText = isCustomer ? "Bestellen" : "(Log in om te bestellen)";
			this.GridView1.HeaderRow.Cells[8].Text = headerText;

			foreach (GridViewRow row in this.GridView1.Rows)
			{
				HiddenField d = row.Cells[0].FindControl("fldDescription") as HiddenField;
				if (d.Value.Length > 0)
				{
					Image img = new Image();
					img.ImageUrl = "/Images/Warning32x32.png";
					img.ToolTip = d.Value;
					row.Cells[0].Controls.Add(img);
				}
			}
		}

		private void SetNumberOrdered()
		{
			if (isCustomer && this.OrderBasketData.Count > 0)
			{
				foreach (GridViewRow row in this.GridView1.Rows)
				{
					HiddenField o = row.Cells[0].FindControl("fldOfferId") as HiddenField;
					int offerId = Convert.ToInt32(o.Value);

					OrderBasketDataItem offer = this.OrderBasketData.GetOffer(offerId);
					if (offer != null)
					{
						DropDownList ddl = row.Cells[8].FindControl("ddlOrderNumber") as DropDownList;
						ddl.SelectedValue = offer.Number.ToString();
					}
				}
			}
		}

		protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if (e.Row.RowType == DataControlRowType.DataRow)
				e.Row.Cells[8].Enabled = isCustomer;
		}

		protected void ddlOrderNumber_SelectedIndexChanged(object sender, EventArgs e)
		{
			DropDownList ddl = (DropDownList)sender;
			TableCell cell = ddl.Parent as TableCell;
			int numOrdered = Convert.ToInt32(ddl.SelectedValue);

			GridViewRow row = cell.Parent as GridViewRow;
			HiddenField o = row.Cells[0].FindControl("fldOfferId") as HiddenField;
			HiddenField d = row.Cells[0].FindControl("fldDescription") as HiddenField;

			int offerId = Convert.ToInt32(o.Value);
			string description = d.Value;

			string vendor;
			decimal price;
			decimal credit;
			int left;
			Panel errorMsgPnl = (Panel)cell.FindControl("pnlErrorMsg");
			Label errorMsgLbl = (Label)errorMsgPnl.FindControl("lblErrorMsg");

			try
			{
				//DataRow offerRow = ActiveOffersPerVendor.Instance(vendorId).GetOffer(offerId);
				var offer = ActiveOffersPerVendor.Instance(vendorId).GetOffer(offerId);
				vendor = this.vendorInfo.BedrijfsNaam;
				price = offer.Price;// (decimal)offerRow["Price"];
				credit = offer.Credit; // (decimal)offerRow["Credit"];
				left = offer.NumberLeft; // (int)offerRow["NumberLeft"];
				if (numOrdered > left)
				{
					throw new Exception("Slechts " + left.ToString() + " beschikbaar");
				}

				errorMsgPnl.Visible = false;
				this.OrderBasketData.Add(offerId, vendor, credit, price, numOrdered, vendorId, description);
				this.OrderBasketControl.GridView.DataSource = this.OrderBasketData;
				this.OrderBasketControl.GridView.DataBind();

				//this.OrderBasketControl.SetEmpty(this.OrderBasketData.Rows.Count == 0);
			}
			catch (Exception ex)
			{
				errorMsgLbl.Text = ex.Message;
				errorMsgPnl.Visible = true;

				ActiveOffersPerVendor.RemoveFromCache(vendorId);
			}
		}

		protected SortDirection SortDir
		{
			get
			{
				if (ViewState["dirState"] == null)
					ViewState["dirState"] = SortDirection.Ascending;

				return (SortDirection)ViewState["dirState"];
			}
			set
			{
				ViewState["dirState"] = value;
			}
		}

		protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
		{
			SortDir = SortDir == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending;
			List<ActiveOffersToBind> list = ActiveOffersPerVendor.Instance(vendorId).Sort(e.SortExpression, SortDir == SortDirection.Ascending);
			this.GridView1.DataSource = list;
			this.GridView1.DataBind();

			this.CustomizeView();
			this.SetNumberOrdered();
		}
	}
}